import requests
from bs4 import BeautifulSoup
import re	
import json


def getHTMLText(url):
    try:
        headers = {'Referer':'https://accounts.pixiv.net/loginlang=zh&source=pc&view_type=page&ref=wwwtop_accounts_index',
           'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.146 Safari/537.36'
          }
        r = requests.get(url, timeout=30, headers=headers)
        r.raise_for_status()
        r.encoding = r.apparent_encoding
        return r.text
    except:
        print("获取网页内容失败！")

def parsePage(html,code):
    list = [] # 我用的二维数组存储
    read = []
    comment  = []
    title  = []
    author  = []
    time  = []
    try:
        # print(html)
        soup = BeautifulSoup(html, "html.parser")
        titles = soup.select("body")
        mycontent =titles[0].contents
        content =mycontent[10].text
        content_list = content.split('\n')
        json_str = content_list[3][10:-1] #获取数祖中第四个元素的字符，并删除掉  获取到的字符串的一些字符 以组成json格式
        content_dict = json.loads(json_str)
        curr_price = content_dict['current']
        print(code+"^"+curr_price+"|",end="")

    except:
        print("解析网页字段失败！")
#上证50 select * 
code_list =['600000','600028','600036','600104','600309','600547','600588','600745','600887','601012','601138','601288','601398','601668','601857','601995','603501','600009','600030','600048','600196','600438','600570','600690','600809','600893','601066','601166','601318','601601','601688','601888','603259','603986','600016','600031','600050','600276','600519','600585','600703','600837','600918','601088','601211','601336','601628','601818','601899','603288']


for code in code_list:
   # print("ticker:"+code)
    parsePage(getHTMLText("https://xueqiu.com/S/SH"+code),code)





    